package Demo06.Demo05;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
 
 
public class Demo06_Test4 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入关键字：");
        String string=scanner.next();
 
        File file=new File("G:\\myFiles");
        FileInputStream fis=null;
        File[] files=file.listFiles();
        for (File file2 : files) {
            if(file2.isFile()){
                try {
                    fis=new FileInputStream(file2);
                    byte[] bs=new byte[50];
                    int count=0;
                    String string2="";
                    while((count=fis.read(bs))!=-1){
                        string2+=new String(bs, 0, count);
                    }

                    if(string2.contains(string)){
                        System.out.println(file2+"  包含关键字："+string);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fis!=null){
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
 
        }
        scanner.close();
 
    }
}
